<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/14
 * Time: 8:24
 */

namespace app\home\controller;

use app\common\controller\Backend;
use think\Db;

class Deploy extends Backend
{
    protected $hsModel = 'Deploy';


    public function getLst()
    {
        $data = Db::name($this->hsModel)->where('deleted',null)->field('id,fid,name')->select();
        $data = $this->tree($data,'id','fid');
        return $data;
    }

    /**
     * @title 获取所有选项配置
     * @description 接口说明
     * @author jyh
     * @url /home/deploy/getDeploy
     * @method POST
     *
     */
    public function getDeploy()
    {
        if ($this->request->isPost()) {
            //筛选条件id
            $param = $this->request->param();
            $page = (int)input('page', 1);
            $limit = (int)input('limit', 10);
            $where = [];
            $like = '';
            $where['fid'] = ['<>',0];
            if (isset($param['sid'])) $where['fid'] = $param['sid'];
            if (isset($param['keyword'])) {
                $like = "u.name like '%{$param['keyword']}%' OR d.name like '%{$param['keyword']}%'";
            }
            $field = $this->model()->where('fid',0)->field('id,name')->select();
            $data = $this->model()
                ->alias('d')
                ->join('user u','d.creator = u.id','left')
                ->where($where)
                ->where($like)
                ->field('d.`id`,d.`name`,d.`fid`,u.`name` as creator,d.`ctime`')
                ->order('d.ctime','desc')
                ->paginate([
                    'page' => $page,
                    'list_rows' => $limit,
                ], false)
                ->toArray();

            foreach ($field as $key => $value) {
                foreach ($data['data'] as $k => $v) {
                    if ($value['id'] == $v['fid']) {
                        $data['data'][$k]['filed'] = $value['name'];
                    }
                }
            }
            return json(['code' => 0, 'msg' => 'success', 'data' => $data,'field' => $field]);
        }
    }

    public function add()
    {
        return parent::add(); // TODO: Change the autogenerated stub
    }

    public function del($ids = "")
    {
        // 判断删除配置选项是是否有关联的需求
        $params = $this->model()->where('id',$ids)->find();
        $deployArr = [
            1 => 'source', // 需求来源
            2 => 'type', // 需求类型
            3 => 'type',// bug类型
            4 => 'priority',// 需求优先级
            5 => 'system',// 操作系统
            6 => 'browser'// 浏览器
        ];
        $table = in_array($params['fid'],[1,2,4]) ? 'ProjectNeed' : 'ProjectBug';
        // 查询删除的选项配置是否有关联数据
        $res = Db::name($table)->where($deployArr[$params['fid']],$params['id'])->find();
        if ($res) {
            return json(['code' => 1, 'msg' => '有关联需求无法删除']);
        }

        return parent::del($ids); // TODO: Change the autogenerated stub
    }

    public function edit($ids = "")
    {
        return parent::edit($ids); // TODO: Change the autogenerated stub
    }
}